<template>
	<view>
		<!-- 上半部分 -->
		<view class="title_image">
			<image :src="goodsInfo.cover_url"></image>
			<h3 style="text-align: left;">{{goodsInfo.title}}</h3>
			<view class="title_text">
				<text style="color: red;font-size: 38rpx;">$ {{goodsInfo.price}}</text>
				<text>销量 {{goodsInfo.sales}}</text>
			</view>
		</view>
		<!-- 下半部分 -->
		<view class="detail">
			<u-tabs lineWidth='40' :itemStyle="style":list="list1" @click="click"></u-tabs>
			<!-- 详情 -->
			<view class="goodsDesc" v-if="show===0">
				<u-parse :content="goodsInfo.details"></u-parse>
			</view>
			<!-- 评论 -->
			<view v-else-if="show===1">
				<view class="comment" v-for="(res, index) in comments" :key="res.id">
					<view class="left"><image :src="res.url" mode="aspectFill"></image></view>
					<view class="right">
						<view class="top">
							<view class="name">{{ res.name }}</view>
							<view class="like" :class="{ highlight: res.isLike }">
								<view class="num">{{ res.likeNum }}</view>
								<u-icon v-if="!res.isLike" name="thumb-up" :size="30" color="#9a9a9a" @click="getLike(index)"></u-icon>
								<u-icon v-if="res.isLike" name="thumb-up-fill" :size="30" @click="getLike(index)"></u-icon>
							</view>
						</view>
						<view class="content">{{ res.contentText }}</view>
						<view class="reply-box">
							<view class="item" v-for="(item, index) in res.replyList" :key="item.index">
								<view class="username">{{ item.name }}</view>
								<view class="text">{{ item.contentStr }}</view>
							</view>
							<view class="all-reply" @tap="toAllReply" v-if="res.replyList != undefined">
								共{{ res.allReply }}条回复
								<u-icon class="more" name="arrow-right" :size="26"></u-icon>
							</view>
						</view>
						<view class="bottom">
							{{ res.date }}
							<view class="reply">回复</view>
						</view>
					</view>
				</view>
				<view v-if="countComments===0">
					暂无评论
				</view>
			</view>
			<!-- 推荐商品 -->
			<view v-else>
				<goods-cart :goods='likeGoods' :loading="loading"></goods-cart>
			</view>
		</view>
		<!-- 底部 -->
		<view style="display: flex;" v-if="show!==2">
			<view class="navigation">
				<view class="left">
					<view class="item u-text-center" style="text-align: center;" @click="collect" >
						<block v-if="is_collect===0">
							<u-icon name="star" :size="25" :color="$u.color['contentColor']"></u-icon>
							<view class="text u-line-1" >收藏</view>
						</block>
						<block v-else>
							<u-icon name="star" :size="25" color='red'></u-icon>
							<view class="text u-line-1" style="color: red;">已收藏</view>
						</block>
					</view>
						<view class="item car u-text-center" style="text-align: center;" @click="goCart">
							<u-badge class="car-num" :value="cartCount" type="warning" :offset="[-3, -6]"></u-badge>
							<u-icon  name="shopping-cart" :size="25" :color="$u.color['contentColor']"></u-icon>
							<view class="text u-line-1">购物车</view>
						</view>
				</view>
				<view class="right" >
					<view class="cart btn u-line-1" @click="addCarts" style="font-size: 12px;!important">加入购物车</view>
					<view class="buy btn u-line-1" @click="toastAlert">立即购买</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loading:false,
				show:0,//展示哪个页面
				goodsId: '',//商品id 点击商品时传过来
				goodsInfo: {},//商品信息
				comments: [],//商品评论
				likeGoods: [],//特推荐商品
				countComments: 0,//评论总数
				//tabs
				list1: [{
					name: '商品详情',
				}, {
					name: '商品评论',
					 badge: {
				   value: 0,
					 }
				}, {
					name: '推荐商品'
				}],
				//tabs样式
				style:{
					width:25 +'%',
					height:40+'px'
				},
				//收藏
				is_collect:0,
				//购物车
				is_on:0,
				is_login:0,
				cartCount:0,
			}
		},
		onLoad(option) {
			this.goodsId = option.id
			this.getData()
			this.getCartCount()
		},
		methods: {
			//获取数据
			async getData() {
				let res = await this.$u.api.goodsDetail(this.goodsId)
				this.goodsInfo = res.goods
				this.comments = res.goods.comments
				this.countComments = res.goods.comments.length
				this.list1[1].badge.value = parseInt(res.goods.comments.length)
				this.is_collect = res.goods.is_collect
				this.is_on = res.goods.is_on
				this.likeGoods = res.like_goods
			},
			//切换tabs
			click(item){
				this.show = item.index
			},
			//收藏
			async collect(){
				this.$u.utils.isLogin()
				await this.$u.api.collectGoods(this.goodsId)
				if(this.is_collect===0){
					this.$u.toast('收藏成功')
					//改变样式
					this.is_collect = 1
				}else{
					this.$u.toast('取消收藏成功')
					//改变样式
					this.is_collect = 0
				}
			},
			//加入购物车
			async addCarts(){
				this.$u.utils.isLogin()
				await this.$u.api.addCarts({goods_id:this.goodsId})
				this.$u.toast('成功')
				this.getCartCount()
			},
			//立即购买
			toastAlert(){
				this.$u.toast('没有钱买不起')
			},
			// 获取购物车数量
			async getCartCount(){
				let res = await this.$u.api.cartCount()
				this.cartCount = res.data.length
			},
			//去购物车
			goCart(){
				this.$u.route({
					url:'pages/cart/cart',
					type:'switchTab'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.box{
			width: 32px; 
			height: 15px;
			background-color: #909193;
			border-radius: 15px;
		}
	.title_image {
		text-align: center;
		padding: 20rpx;
		padding-bottom: 0px;
	}

	.title_image image {
		height: 400rpx;
		width: 300rpx;
	}

	.title_text {
		display: flex;
		justify-content: space-between;
	}
	.detail{
		text-align:center;
	}
	.goodsDesc{
		margin-bottom: 100rpx;
	}
	.comment {
		display: flex;
		padding: 30rpx;
		.left {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}
		.right {
			flex: 1;
			padding-right: 0rpx;
			font-size: 30rpx;
			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;
				.name {
					color: #5677fc;
				}
				.like {
					display: flex;
					align-items: center;
					color: #9a9a9a;
					font-size: 26rpx;
					.num {
						margin-right: 4rpx;
						color: #9a9a9a;
					}
				}
				.highlight {
					color: #5677fc;
					.num {
						color: #5677fc;
					}
				}
			}
			.content {
				margin-bottom: 10rpx;
			}
			.reply-box {
				background-color: rgb(242, 242, 242);
				border-radius: 12rpx;
				.item {
					padding: 20rpx;
					border-bottom: solid 2rpx $u-border-color;
					.username {
						font-size: 24rpx;
						color: #999999;
					}
				}
				.all-reply {
					padding: 20rpx;
					display: flex;
					color: #5677fc;
					align-items: center;
					.more {
						margin-left: 6rpx;
					}
				}
			}
			.bottom {
				margin-top: 20rpx;
				display: flex;
				font-size: 24rpx;
				color: #9a9a9a;
				.reply {
					color: #5677fc;
					margin-left: 10rpx;
				}
			}
		}
	}
	.navigation {
		display: flex;
		position: fixed;
		justify-content: space-between;
		z-index: 999;
		width: 100%;
		bottom: 0px;
		border: solid 2rpx #f2f2f2;
		background-color: #ffffff;
		padding: 16rpx 0;
		.left {
			display: flex;
			font-size: 20rpx;
			.item {
				margin: 0 30rpx;
				text-align: center;
				&.car {
					text-align: center;
					position: relative;
					.car-num {
						position: absolute;
						top: -10rpx;
						right: -10rpx;
					}
				}
			}
		}
		.right {
			display: flex;
			float: right;
			right: 0;
			font-size: 28rpx;
			align-items: center;
			.btn {
				line-height: 66rpx;
				padding: 0 30rpx;
				border-radius: 36rpx;
				color: #ffffff;
			}
			.cart {
				background-color: #ed3f14;
				margin-right: 30rpx;
			}
			.buy {
				background-color: #ff7900;
			}
		}
	}
</style>
